<%@ page language="java" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
${aier_doctype}
<html <s:property value="aier_html_xmlns" escape="false"/>>
<head>
<meta http-equiv="Content-Type" content="${aier_content_type}" />
<title><s:property value="ev_title" /></title>
<meta name="keywords" content="${aier_keyworks}" />
<meta name="description" content="${aier_description}" />
<link href="${aier_css_url}jq/jq.css" rel="Stylesheet" />
<link rel="stylesheet" href="${aier_css_url}${aier_jqui_css}/ui.css" />
<link rel="stylesheet" href="${aier_css_url}social/module/jq-msg.css" />
<link rel="stylesheet" href="${aier_css_url}comm/text.css" />
<link rel="stylesheet" href="${aier_css_url}admin/admin.css" />

<script type="text/javascript" src="${aier_js_url}j.js"></script>
<script type="text/javascript" src="${aier_js_url}b.js"></script>
<script type="text/javascript" src="${aier_js_url}jp/msg.js"></script>
<script type="text/javascript" src="${aier_js_url}jp/1.js"></script>
<script type="text/javascript" src="${aier_js_url}lib/afu.js"></script>
<script type="text/javascript" src="${aier_js_url}jp/hh/ui.js"></script>
<script type="text/javascript" src="${aier_js_url}lib/block.js"></script>
<script type="text/javascript" src="${aier_js_url}lib/arrayList.js"></script>
<script type="text/javascript" src="${aier_js_url}jc/cj.js"></script>
<script type="text/javascript" src="${aier_js_url}jq/jq.js"></script>
<script type="text/javascript" src="${aier_js_url}jq/cn.js"></script>
<script type="text/javascript" src="${aier_js_url}comm/text.js"></script>
</head>

<body style="background-color: #EEF2FB;">
    
    <div style="background-color: #EEF2FB;width: 100%;height: 700px; ">
       
         <div id="tabs">
             <ul>
                 <li><a href="#tabs-1">代理商管理</a></li>
             </ul>
             <div id="tabs-1" style="height: 500px;">
                 <div class="d_content">
                    <button onclick="history.go(-1);" style="margin-left: 700px;"
                        class="b_maSel ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
                        role="button" aria-disabled="false">
                        <span class="ui-button-text">返回</span>
                    </button>
                    <div class="d_scnOut">
                        <div class="d_scname" style="font-size: 14px;">
                            <label>名称:</label>
                        </div>
                        <div class="d_sctext" style="font-size: 14px;">
                            <input type="text" id="info_name" class="txt_scname"><span
                                id="span_name"></span>
                        </div>
                    </div>
                    <div class="d_scnOut" style="margin-top: -40px;margin-left: 400px;">
                        <div class="d_scname" style="font-size: 14px;">
                            <label>联系邮箱:</label>
                        </div>
                        <div class="d_sctext" style="font-size: 14px;">
                            <input type="text" id="info_email" class="txt_scname"> <span
                                id="span_email"></span>
                        </div>
                    </div>
                    <div class="d_scnOut">
                           <div class="d_scname" style="font-size: 14px;">
                               <label for="info_phone">联系电话:</label>
                           </div>
                           <div class="d_sctext" style="font-size: 14px;">
                               <input type="text" id="info_phone" class="txt_scname"> <span
                                   id="span_phone"></span>
                           </div>
                           <div id="txt_phone_show"
                               style="margin-left:280px;margin-top:-15px;font-size: 12px;color: red;*margin-left:290px;">如是固定电话,格式为"区号-号码"。如要加入多个号码,请用","分隔。</div>
                    </div>
                    <div style="margin-left: 0px;margin-top: 0px; height: 200px;overFlow-y: auto ;">
                        <div class="d_scnOut">
                            <div class="d_scname" style="font-size: 14px;">
                                <label>代理区域:</label>
                            </div>
                            <div style="font-size: 14px; margin-left:120px;margin-top: -18px;" id = "pqy">
                            </div>
                            <div class="sel_region" style ="margin-top: 10px;" id = "region">
                                <s:action name="cityutil" namespace="/common"
                                    executeResult="true">
                                    <s:param name="cityid">-1</s:param>
                                    <s:param name="dvid">region</s:param>
                                </s:action>
                                <button id="addpay"onclick="addPay();">添加代理区域
                                   </button>
                            </div>
                        </div>
                    </div>
                    <button id="b_siUpdate"
                        class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
                        role="button" aria-disabled="false"
                        style="left: 300px; top: 20px;" onclick="update();">
                        <span class="ui-button-text">修改</span>
                    </button>
                    <button id="b_sidelete"
                        class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
                        role="button" aria-disabled="false"
                        style="left: 300px; top: 20px;" onclick="deleteTea()">
                        <span class="ui-button-text">删除</span>
                    </button>
                       <div id="proxy_del" style="display: none;" title="删除代理商">
                                                                                             您确定要删除该代理商吗?
                       </div>
                    <button id="b_siAdd"
                        class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
                        role="button" aria-disabled="false"
                        style="left: 300px; top: 20px;display: none;" onclick="save();">
                        <span class="ui-button-text">保存</span>
                    </button>
                    <button id="b_siCancel"
                        class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
                        role="button" aria-disabled="false"
                        style="left: 300px; top: 20px;display: none;"
                        onclick="cancel()">
                        <span class="ui-button-text">取消</span>
                    </button>
                            
				</div>
			</div>
        </div>
	</div>

	<script type="text/javascript">
	   $("#tabs").tabs();
	   var cidStr;
	   showInfo();
	   // 显示详细信息
	   function showInfo() {
	        var fun = function(data) {
	            var proxy = data.proxy;
		        //名称
	            $('#info_name').val(proxy.name);
	            $('#span_name').show();
	            $('#span_name').html(proxy.name);
	            $("#info_name").hide();
	            //邮箱
	            $('#info_email').val(proxy.email);
	            $('#span_email').show();
	            $("#span_email").html(proxy.email);
	            $("#info_email").hide();
	            
	            //联系电话
	            $('#info_phone').val(proxy.phone);
	            $('#span_phone').css("visibility", "visible");
	            $("#span_phone").html(proxy.phone);
	            $("#info_phone").hide();
	            $("#txt_phone_show").hide();
	            
	            $("#b_siUpdate").show();
	            $("#b_siClose").show();
	            $("#b_siAdd").hide();
	            $("#b_siCancel").hide();
	            var proxyc = data.proxyc;
	            var html = "";
	            cidStr = "";
	            if (null == proxyc || proxyc.length == 0) {
	                html = "暂无代理区域";
	            } else {
		            for (var i=0;i<proxyc.length;i++) {
		                if ("" != cidStr) {
		                    cidStr += ",";
		                }
		                cidStr += proxyc[i].cid;
		                html+="<div id='address"+proxyc[i].cid+"'>"+"<span>"+proxyc[i].cname+"</span>"+"<button id='but"+proxyc[i].cid+"'"+" onclick='delAddress("+proxyc[i].cid+")'>删除</button></div>";
		            }
	            }
	            $('#pqy').html(html);
	            
	            //代理区域
                $('#region').hide();
                for (var i=0; i<cidStr.split(",").length; i++) {
                    $("#but"+cidStr.split(",")[i]).attr("disabled","true");
                }
	            
	        };
	        exeAjax(
	                ajaxUrl("${aier_url}${aier_urijs}adminproxy_searchProxyInfo${aier_suffix}"),
	                fun, {
	                    "pid" : "${pid}",
	                });
	
	    }
	    //修改按钮事件
	    function update() {
            //名称
            $("#info_name").show();
            $('#span_name').hide();
            
            //邮箱
            $("#info_email").show();
            $("#span_email").hide();
            
            //联系电话      
            $("#info_phone").show();
            $("#txt_phone_show").show();
            $("#span_phone").css("visibility", "hidden");
            
            //代理区域
            $('#region').show();
            $("#pqy :button").removeAttr("disabled");
            
            
	        $("#b_siUpdate").hide();
	        $("#b_siClose").hide();
	        $("#b_sidelete").hide();
	        $("#b_siAdd").show();
	        $("#b_siCancel").show();
	
	    }
	    
	    // 取消按钮
	    function cancel() {
	        showInfo();
	        load(-1);
	        $("#b_siUpdate").show();
	        $("#b_sidelete").show();
	        $("#b_siAdd").hide();
	        $("#b_siCancel").hide();
	    }
	    
	    // 保存修改信息
	    function save() {
            var name = $('#info_name').val();
            var email = $('#info_email').val();
            var phone = $('#info_phone').val();
            
            if(isnull(name.trim())){
                $().message("请输入代理商名称!");
                return          
            }
            if(email.trim()!=""&&check_email(email.trim())=="false"){
                $().message("邮箱"+email+"格式有误!");
                return
            }
            var phonevalue = check_phone(phone);
            if(phonevalue!="true"){
                $().message("号码"+phonevalue+"格式有误!");
                return
            }   
            
            var fun = function(data) {
                if (data.s == 1) {
                    $().message("修改成功!");
                    $("#proxyinfo").trigger("reloadGrid");
                    cancel();

                } else {
                    $().message("修改失败!");
                }
            };
            exeAjax(
                    ajaxUrl("${aier_url}${aier_urijs}adminproxy_saveProxyInfo${aier_suffix}"),
                    fun, {
                        "pid" : "${pid}",
                        "email" : email,
                        "proxyName" : name,
                        "phone" : phone,
                        "cidStr" : cidStr
                    });
	    }
	    
	    // 添加代理区域
        function addPay() {
            var pcid = -1;
            if(acity.cid>-1){
                pcid = acity.cid;
            }
            if (pcid == -1) {
               $().message("请选择区域!");
               return;
            }
            for (var i=0;i<cidStr.split(",").length;i++) {
                if (cidStr.split(",")[i] == pcid) {
                    $().message("已有相同区域!");
                    return;
                }
            }
            
            var address = "";
            if ($('#s_region_p  option:selected').val() != -1) {
                address += $('#s_region_p  option:selected').text();
            }
            if ($('#s_region_c  option:selected').val() != -1) {
                address += $('#s_region_c  option:selected').text();
            }
            if ($('#s_region_a  option:selected').val() != -1) {
                address += $('#s_region_a  option:selected').text();
            }
            if ($('#s_region_t  option:selected').val() != -1) {
                address += $('#s_region_t  option:selected').text();
            }
            var html = "<div id = 'address"+pcid+"'>"+"<span>"+address+"</span>"+"<button onclick='delAddress("+pcid+")'>删除</button></div>";
            if ($('#pqy').html() == "暂无代理区域") {
                $('#pqy').html(html);
            } else {
                $('#pqy').html($('#pqy').html()+html);
                cidStr += ",";
            }
            cidStr += pcid;
            load(-1);
            acity.cid = -1;
        }
        
        // 删除代理区域
        function delAddress(cid) {
            var str = "";
            for (var i=0;i<cidStr.split(",").length;i++) {
                if (cidStr.split(",")[i] != cid) {
                    if (str != "") {
                        str+=",";
                    }
                    str += cidStr.split(",")[i];
                }
            }
            cidStr = str;
            var div = document.getElementById("address"+cid);
		    if (div != null) {
		        div.parentNode.removeChild(div);
		    }
		    if (null == cidStr || cidStr == "") {
                $('#pqy').html("暂无代理区域");
            }
		}
		
		// 删除代理商
		function deleteTea(){
	        $(function() {
	            $( "#proxy_del" ).dialog({
	                modal: true,
	                resizable: false,
	                buttons: {
	                    "确定": function() {
	                        var fundel = function(data1){
                                if(1==data1.s){
                                     $().message("删除成功!");
                                     $("#proxy_del").dialog( "close" );
                                     history.go(-1);
                                }else{
                                     $().message("删除失败!");
                                }
                            };
                            exeAjax(ajaxUrl("${aier_url}${aier_urijs}adminproxy_deleteProxy${aier_suffix}"), fundel, {"pid" :"${pid}"});
	                    },"取消": function() {
	                        $( this ).dialog( "close" );
	                    }
	                }
	            });
	        });     
	                
	    }
	    
	</script>
</body>
</html>